package com.samsung.actividades;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.database.Cursor;
import android.sax.StartElementListener;
import android.widget.EditText;

import com.samsung.R;
import com.samsung.database.PartesDBAdapter;

public class Logs {
	
	File myFile;
	 private PartesDBAdapter partesDBAdapter;
	 Cursor partesCursor;
	 
	public static void main(String[] args) throws IOException{
		//Log log=new Log();
		//log.setLog("25","Visita","Se realizo la visita pero no habia nadie");		
	}
	
	
	
	public boolean setLog(String ruta, String NoOrden, String Proceso,
			String estado, String sintoma, String modelo, String img1,
			String img2) {
		String texto="";
		try {
			texto = existefile(ruta) + fecha_actual() + ";" + NoOrden
					+ ";" + Proceso + ";" + estado + ";"
					+ sintoma + ";" + modelo + ";" + img1 + ";" + img2;
			escribir(texto);
			return true;
		} catch (IOException e) {
			System.out.println("Error set log"+texto+"/n"+e.toString());
			return false;
		}
	}
	
	private String fecha_actual() {		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(new Date());
	}
	
	public String getfecha() {		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(new Date());
	}
	
	public String gethora() {		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		return formatter.format(new Date());
	}
	
//	private String get_ruta(){
//		//return "/sdcard/";
//		partesCursor = partesDBAdapter.getRuta();		
//		if(partesCursor.moveToFirst()){
//			String salida= partesCursor.getString(partesCursor.getColumnIndex(PartesDBAdapter.KEY_VALOR));
//			if(salida.equals("")) return "/sdcard/";
//			else return salida;
//		}
//		else return "/sdcard/";
//	}

	private String existefile(String ruta) throws IOException{
		//myFile = new File("/sdcard/log"+fecha()+".csv");
		myFile = new File(ruta+"log"+getfecha()+".csv");
		if(!myFile.exists()) {
			myFile.createNewFile();					
			return "Fecha;No Orden;Proceso;Estado;sintoma;modelo;img1;img2\n";			
		}
		else return leer();
	}
	
	private String leer() throws IOException{
		FileInputStream fIn = new FileInputStream(myFile);
		BufferedReader myReader = new BufferedReader(
				new InputStreamReader(fIn));
		String aDataRow = "";
		String aBuffer = "";
		while ((aDataRow = myReader.readLine()) != null) {
			aBuffer += aDataRow + "\n";
		}
		myReader.close();
		return aBuffer;
	}
	
	private void escribir(String tex) throws IOException{
		FileOutputStream fOut = new FileOutputStream(myFile);
		OutputStreamWriter myOutWriter = new OutputStreamWriter(
				fOut);					
		myOutWriter.append(tex);
		myOutWriter.close();
		fOut.close();		
	}


}
